package com.itheima.service.impl;

import com.itheima.dao.SetmealDao;
import com.itheima.dto.PageResult;
import com.itheima.dto.QueryPageBean;
import com.itheima.dto.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.OrderService;
import com.itheima.service.SetmealCGSerivce;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private SetmealCGSerivce setmealCGSerivce;
    @Autowired
    private OrderService orderService;

    // 分页条件查询
    @Override
    public PageResult selectfindPage(QueryPageBean pageBean) {
        Integer pageSize = pageBean.getPageSize();
        Integer page = (pageBean.getCurrentPage() - 1) * pageSize;
        String sname = pageBean.getQueryString();
        PageResult pageResult = new PageResult();
        ArrayList<Setmeal> list = setmealDao.selectfindPage(page, pageSize, sname);
        Long count = setmealDao.selectfindPageCount(sname);
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }

    /*
     * 添加套餐
     * 前端传回setmeal以及检查组的id
     * */
    public void addSetmeal(Setmeal setmeal, Integer[] checkgroupIds) {
        setmealDao.addSetmeal(setmeal);
        //获取套餐的id
        Integer id = setmeal.getId();
        for (Integer checkgroupId : checkgroupIds) {
            Map<String, Integer> map = new HashMap<>();
            map.put("setmealId", id);
            map.put("checkgroupId", checkgroupId);
            setmealDao.addCheckgroupId(map);
        }

        //setmealDao.addCheckgroupId(id,checkgroupIds);
    }

    @Override
    public Setmeal selectSetmealById(Integer id) {
        Setmeal setmeal=setmealDao.queryById(id);
        return setmeal;
    }

    /*
    * 根据套餐id查询对应的检查组id集合
    * */
    @Override
    public List<Integer> queryCheckgroupIdsBySetmealId(Integer id) {
        List<Integer> ids=setmealDao.queryCheckgroupIdsBySetmealId(id);
        return ids;
    }

    // 按id删除
    @Override
    @Transactional
    public Result deleteById(@PathVariable Integer setmealId) {
        //this.deleteById(setmealId);
        int i = setmealCGSerivce.selectById(setmealId);
        if (i>0){
        setmealCGSerivce.deleteById(setmealId);
        }
        setmealDao.deleteById(setmealId);
        return Result.success("删除套餐成功");
    }

    @Override
    public void updateSetmeal(List<Integer> checkgroupIds, Setmeal setmeal) {
        //获取到套餐的id
        Integer setmealId = setmeal.getId();
        //根据套餐的id删除已有的
        setmealDao.deleteCheckgroupById(setmealId);
        setmealDao.updateSetmeal(setmeal);
        for (Integer checkgroupId : checkgroupIds) {
            setmealDao.saveCheckGroupAndSetmeal(setmealId,checkgroupId);
        }

    }

}
